﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.QuanLyKho;

namespace QuanLyKho.Control
{
    public partial class Kho_Chuyenkho : UserControl
    {
        public Kho_Chuyenkho()
        {
            InitializeComponent();
            LoadData();
            SetPropertiesGrid();
        }

        public void SetPropertiesGrid()
        {
            gvChuyenKho.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvChuyenKho.OptionsView.ShowFooter = true;
            gvChuyenKho.OptionsFind.AlwaysVisible = true;
            gcID.Visible = false;

            gvDuyet.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvDuyet.OptionsView.ShowFooter = true;
            gvDuyet.OptionsFind.AlwaysVisible = true;
            gcID1.Visible = false;
        }

        private void LoadData()
        {
            gridChuyenKho.DataSource = Data.DataAll.SelectAll("ChuyenKho_VatTu",
                "*,(select TenKho from [dbo].[Kho_KhoHang] where [Kho_KhoHang].IDKhoHang = [dbo].[ChuyenKho_VatTu].PhongChuyen) as 'PhongChuyen1'," +
                "(CASE WHEN (select TenKho from [dbo].[Kho_KhoHang] where [Kho_KhoHang].IDKhoHang = [dbo].[ChuyenKho_VatTu].PhongNhan) is null THEN N'Trả về kho tổng' ELSE (select TenKho from [dbo].[Kho_KhoHang] where [Kho_KhoHang].IDKhoHang = [dbo].[ChuyenKho_VatTu].PhongNhan) END) as 'PhongNhan1'",
                "Where TrangThai = " + Convert.ToInt32(TrangThaiKho.ChuyenKho_TaoMoi) + " OR TrangThai = " + Convert.ToInt32(TrangThaiKho.ChuyenKho_KhongDuyet));

            gridDuyet.DataSource = Data.DataAll.SelectAll("ChuyenKho_VatTu",
                "*,(select TenKho from [dbo].[Kho_KhoHang] where [Kho_KhoHang].IDKhoHang = [dbo].[ChuyenKho_VatTu].PhongChuyen) as 'PhongChuyen1'," +
                "(CASE WHEN (select TenKho from [dbo].[Kho_KhoHang] where [Kho_KhoHang].IDKhoHang = [dbo].[ChuyenKho_VatTu].PhongNhan) is null THEN N'Trả về kho tổng' ELSE (select TenKho from [dbo].[Kho_KhoHang] where [Kho_KhoHang].IDKhoHang = [dbo].[ChuyenKho_VatTu].PhongNhan) END) as 'PhongNhan1'",
                "Where TrangThai = " + Convert.ToInt32(TrangThaiKho.ChuyenKho_ChoDuyet) + " OR TrangThai = " + Convert.ToInt32(TrangThaiKho.ChuyenKho_DaDuyet));
        }

        private void gvChuyenkho_DoubleClick(object sender, EventArgs e)
        {
            Kho_Themchuyenkho.ID_ChuyenKho = gvChuyenKho.Columns["ID"].View.GetRowCellDisplayText(gvChuyenKho.FocusedRowHandle, "ID");
            Kho_Themchuyenkho themnhapkho = new Kho_Themchuyenkho();
            themnhapkho.ShowDialog();
            LoadData();
            Kho_Themchuyenkho.ID_ChuyenKho = "0";
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            Kho_Themchuyenkho themchuyenkho = new Kho_Themchuyenkho();
            themchuyenkho.ShowDialog();
            LoadData();
        }

        private void btnGuiPhieu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvChuyenKho.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn gửi lệnh chuyển kho này.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Update("ChuyenKho_VatTu", "TrangThai =" + Convert.ToInt32(TrangThaiKho.ChuyenKho_ChoDuyet) + " WHERE ID =" + dataview[gvChuyenKho.FocusedRowHandle]["ID"]);

                MessageBox.Show(message.Equals("success") ? "Gửi lệnh thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                LoadData();
            }
        }

        private void gvChuyenKho_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiKho(Convert.ToInt32(e.Value));
            }
        }

        private void gvDuyet_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiKho(Convert.ToInt32(e.Value));
            }
        }

        private void gvDuyet_DoubleClick(object sender, EventArgs e)
        {
            Kho_Themchuyenkho.ID_ChuyenKho = gvDuyet.Columns["ID"].View.GetRowCellDisplayText(gvDuyet.FocusedRowHandle, "ID");
            Kho_Themchuyenkho themnhapkho = new Kho_Themchuyenkho();
            themnhapkho.ShowDialog();
            LoadData();
            Kho_Themchuyenkho.ID_ChuyenKho = "0";
        }
    }
}
